package com.boardour.wifip2p;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.wifi.p2p.WifiP2pConfig;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Build;
import android.provider.Settings;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;

import com.boardour.wifip2p.core.IWifiP2pCommon;
import com.boardour.wifip2p.utils.LG;

import static android.os.Looper.getMainLooper;

public class WifiP2pCommon implements IWifiP2pCommon, WifiP2pManager.ChannelListener {
    public static final int SERVICE_PORT = 8595;
    public static final int SERVICE_AAC_PORT = 8596;
    private static String TAG = "WifiP2pCommon";
    private static WifiP2pCommon instance = new WifiP2pCommon();
    private Context mContext;
    private WifiP2pManager mWifiP2pManager;
    private WifiP2pManager.Channel mChannel;

    private WifiP2pCommon() {
    }

    public static WifiP2pCommon getInstance() {
        return instance;
    }

    public void init(Context context, boolean isDebug) {
        LG.isDebug = isDebug;
        this.mContext = context;
        mWifiP2pManager = (WifiP2pManager) mContext.getSystemService(Context.WIFI_P2P_SERVICE);
        mChannel = mWifiP2pManager.initialize(mContext, getMainLooper(), this);
    }

    public void init(Context context) {
        init(context, false);
    }

    @Override
    public void onChannelDisconnected() {
        LG.e(TAG, "onChannelDisconnected");
    }


    @Override
    public void createGroup(final WifiP2pManager.ActionListener listener) {
        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(mContext, "need permission ACCESS_FINE_LOCATION", Toast.LENGTH_SHORT).show();
            return;
        }
        mWifiP2pManager.createGroup(mChannel, listener);
    }

    public void removeGroup() {
        removeGroup(null);
    }

    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    public void requestDeviceInfo(WifiP2pManager.DeviceInfoListener deviceInfoListener) {
        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(mContext, "need permission ACCESS_FINE_LOCATION", Toast.LENGTH_SHORT).show();
            return;
        }
        mWifiP2pManager.requestDeviceInfo(mChannel, deviceInfoListener);
    }

    @Override
    public void removeGroup(WifiP2pManager.ActionListener listener) {
        mWifiP2pManager.removeGroup(mChannel, listener);
    }

    /**
     * 取消和退出群
     */
    public void cancelAndRemoveConnect() {
        cancelConnect(null);
        removeGroup();
    }

    /**
     * 其实就是从组内移除
     */
    @Override
    public void cancelConnect(WifiP2pManager.ActionListener listener) {
        mWifiP2pManager.cancelConnect(mChannel, listener);
    }

    public void connect(WifiP2pConfig config) {
        connect(config, null);
    }

    @Override
    public void connect(WifiP2pConfig config, WifiP2pManager.ActionListener listener) {
        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(mContext, "need permission ACCESS_FINE_LOCATION", Toast.LENGTH_SHORT).show();
            return;
        }
        mWifiP2pManager.connect(mChannel, config, listener);
    }


    @Override
    public void requestConnectionInfo(WifiP2pManager.ConnectionInfoListener listener) {
        mWifiP2pManager.requestConnectionInfo(mChannel, listener);
    }

    public void discoverPeers() {
        discoverPeers(null);
    }

    @Override
    public void discoverPeers(final WifiP2pManager.ActionListener listener) {
        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(mContext, "need permission ACCESS_FINE_LOCATION", Toast.LENGTH_SHORT).show();
            return;
        }
        mWifiP2pManager.discoverPeers(mChannel, listener);
    }

    public void stopPeerDiscovery() {
        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(mContext, "need permission ACCESS_FINE_LOCATION", Toast.LENGTH_SHORT).show();
            return;
        }
        mWifiP2pManager.stopPeerDiscovery(mChannel, null);
    }

    @Override
    public void requestPeers(final WifiP2pManager.PeerListListener listener) {
        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(mContext, "need permission ACCESS_FINE_LOCATION", Toast.LENGTH_SHORT).show();
            return;
        }
        mWifiP2pManager.requestPeers(mChannel, listener);
    }

    /**
     * 打开Wifi开关
     */
    public void startWifiSetting() {
        if (mWifiP2pManager != null && mChannel != null) {
            Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);
        } else {
            Toast.makeText(mContext, "当前设备不支持Wifi Direct", Toast.LENGTH_SHORT).show();
        }
    }

    public static String getDeviceStatus(int deviceStatus) {
        switch (deviceStatus) {
            case WifiP2pDevice.CONNECTED:
                return "已连接";
            case WifiP2pDevice.INVITED:
                return "邀请中";
            case WifiP2pDevice.AVAILABLE:
                return "可用的";
            case WifiP2pDevice.UNAVAILABLE:
                return "不可用的";
            case WifiP2pDevice.FAILED:
                return "失败的";
            default:
                return "未知的";
        }
    }
}
